<?php
/**
 * app 大话水浒
 * @author will
 */
class App_Overlord_GameStart {
	//存在a武将武技闭月斩记录
	static $a_special = null;
	//存在c武将武技闭月斩记录
	static $c_special = null;
	/**
	 * 添加操作
	 * @param unknown_type $key
	 * @param unknown_type $value
	 */
	public static function getAddOverlord($table , $key , $value){
		$time = isset($_SERVER['REQUEST_TIME']) ? $_SERVER['REQUEST_TIME'] : time(); //获取时间
		$Db = loadDB();
		$value = serialize($value);
		//把数据写入数组
		$insert_date = array(
			'Key' => $key,
			'Value' => $value,
			'time' => $time,
		);
		$success = $Db->doInsert($table , $insert_date);
		return $success ? 0 : 901;
	}
	/**
	 * 查询全部操作
	 */
	public static function getAllOverlord($table){
		$Db = loadDB();

		$result = $Db->getAll("SELECT * FROM {$table}");
		
		if(empty($result)) return array();
		if (!empty($result)){
			foreach($result as $k => $row) {
				$result[$k]['Value'] = unserialize($row['Value']);
			}
		}
		return $result;
	}
	/***************************************分表后的操作***************************************/
	/**
	 * 添加操作
	 * @param unknown_type $key
	 * @param unknown_type $value
	 */
	public static function getAddsOverlord($table , $key , $value){
		$time = isset($_SERVER['REQUEST_TIME']) ? $_SERVER['REQUEST_TIME'] : time(); //获取时间
		$temp = ltrim($table , 'app_overlord_');
		
		if ($temp == 'generals'){
			//数据库
			$temp_db = 'app_collocate';
			//表
			$newtable = $table;
		}else {
			$temp_db = 'app_' . $temp;
		}
		$Db = loadDB($temp_db);
		$value = serialize($value);
		//把数据写入数组
		$insert_date = array(
			'Key' => $key,
			'Value' => $value,
			'time' => $time,
		);
		$success = $Db->doInsert($table , $insert_date);
		return $success ? 0 : 901;
	}
	
	/**
	 * 查询单个表
	 */
	public static function getOneOverlord($table , $key){
		$Db = loadDB();
		
		$result = $Db->getOne("SELECT * FROM {$table} WHERE `Key` = '{$key}'");
	
		if(empty($result)) return array();
		$result['Value'] = unserialize($result['Value']);
		
		return $result;
	}
	
	/**
	 * 修改表内容
	 * @param unknown_type $key
	 */
	public static function getAlterOverlord($table , $key , $value){
		$time = isset($_SERVER['REQUEST_TIME']) ? $_SERVER['REQUEST_TIME'] : time(); //获取时间
		$Db=loadDB();
		
		$value = serialize($value);
		$update_data = array(
			'Value' => $value,
			'time' => $time,
		);
	
		$success = $Db->doUpdate($table , $update_data , "`Key` = '{$key}'");
		
		return $success ? 0 : 902;
	}
	/**
	 * 查询游戏人数
	 */
	public static function getCountUser(){
		
		$Db = loadDB();
		$count = $Db->getOne("SELECT count(*) as count FROM `app_overlord_user`");
		return $count;
	}

	/**
	 * 随机获取N个同等级的君主
	 */
	public static function getLevelUser($lv , $num = 4){
		$Db = loadDB('app_collocate');
		$level_index = $Db->getOne("SELECT `Value` FROM `app_overlord_level_user` WHERE `Key` = 'level_user_index'" , 'Value');
		if(empty($level_index)) return array();
		$level_index = unserialize($level_index);
		if(empty($level_index[$lv])) return array();

		// 从索引中随机获取
		$index = 1;
		if( $level_index[$lv] > 1 ) {
			$index = rand(1 , $level_index[$lv]);
		}

		$level_array_key = "level_user_{$lv}_{$index}";
		$result = $Db->getOne("SELECT `Value` FROM `app_overlord_level_user` WHERE `Key` = '{$level_array_key}'" , 'Value');
		if(empty($result)) return array();
		
		$uins = explode(',' , $result);
		if(count($uins) <= $num) return $uins;

		// 随机获取n个
		$array_rand_key = array_rand($uins , min( $num , count($uins)) );
		$return_array = array();
		foreach($array_rand_key as $rand_key) {
			$return_array[] = $uins[$rand_key];
		}
		
		return $return_array;
	}
	

	/**
	 *计算一天的时间 
	 */
	public static function getTime(){
		$time_now=time();
		$time_exp = date("d:H:i:s",$time_now);
		$time_arr = explode(":",$time_exp);
		$time = (($time_arr[1]*60+$time_arr[2])*60+$time_arr[3]);//今天00：00到当前过的秒数
		$now_time = $time_now-$time;//计算当天00:00
		$next_time= $time_now+3600*24-$time;//计算明天00:00
		$is_time = array($now_time,$next_time);
		return $is_time;
	}
	/**
	 * 计算完成时间
	 * @param <int> $end_time
	 */
	public static function getEndDate($end_time){
		$end_time = $end_time -time();
			
		if($end_time <= 0){
			//self::getAlterState($id, $uin, 1);
		}else{
			$end_time_array = $end_time + (16 * 3600); // 因为我们时区是8
			$end_time_array = explode(':' , date('H:i:s' , $end_time_array));
			return $end_time_array;
		}
	}
	/**
	 * 兵种
	 * Enter description here ...
	 */
	public static function getBarracksType($country){
		$type = array();
		
		switch ($country) {
			case 'wu' : $type = array(
					'special' => '蛮夷军',
					'hp' => 10,
					'ap' => 7,
					'defense' => 9,
					); break;
			case 'wei' : $type = array(
					'special' => '神羿兵',
					'hp' => 10,
					'ap' => 9,
					'defense' => 7,
					); break;
			case 'shu' : $type = array(
					'special' => '蛟虎骑',
					'hp' => 10,
					'ap' => 8,
					'defense' => 8,
					); break;
		}
		$type['time'] = 15;
		$list = array(array('infantry' => '步兵' , 'hp' => 10, 'ap' => 7, 'defense' => 8, 'time' => 11), array('cavalryman' => '骑兵' , 'hp' => 10, 'ap' => 8, 'defense' => 7, 'time' => 11), array('toxotae' => '弓箭手' , 'hp' => 10, 'ap' => 7, 'defense' => 7, 'time' => 11), $type);
		return $list;
	}
	/**
	 * 提示页面
	 * Enter description here ...
	 * @param $msg
	 * @param $urls
	 */
	static public function success($msg , $urls = array()) {
		// 如果有post的 back_url，则 自动设置变量
		if(!empty($_POST['back_url'])) {
			// 这里需要对 post 数据做过滤  ..todo
			$urls[] = array(
				'name' => empty($_POST['back_url_name']) ? '返回上一页' : $_POST['back_url_name'],
				'url'  => str_replace('&' , '&amp;' , $_POST['back_url']),
			);
		}
		
		$smarty = loadSmarty();
		$smarty->assign('msg' , $msg);
		$smarty->assign('urls' , $urls);
		
		return $smarty->fetch('app/Overlord/success.tpl.php');
	}
	/**
	 * 新手礼包
	 */
	public static function getNewUser($time){
		$gold = array();
		
		switch ($time) {
			case 0 : $gold = array(
					'time' => 0,
					'prop' => array('training' => array(0 , 1) , 'check' => array(0 , 1) , 'recover' => array(0 , 2) , 'treasure' => array(0 , 2) , 'material' => array(0 , 4))
					); break;
			case 1 : $gold = array(
					'time' => 3,
					'prop' => array('training' => array(0 , 2) , 'recover' => array(0 , 3) , 'recruit' => array(0 , 4) , 'key' => array(0 , 1) , 'material' => array(3 , 2))
					); break;
			case 2 : $gold = array(
					'time' => 9,
					'prop' => array('training' => array(1 , 2) , 'recover' => array(0 , 3) , 'wipe_off' => array(1 , 1) , 'material' => array(1 , 2) , 'treasure' => array(3 , 2))
					); break;
			case 3 : $gold = array(
					'time' => 19,
					'prop' => array('training' => array(1 , 4) , 'recruit' => array(1 , 2) , 'wipe_off' => array(0 , 1) , 'material' => array(4 , 2) , 'key' => array(3 , 1))
					); break;
			case 4 : $gold = array(
					'time' => 59,
					'prop' => array('training' => array(2 , 2) , 'recruit' => array(2 , 1) , 'expedition' => array(1 , 1) , 'material' => array(4 , 2) , 'treasure' => array(4 , 1))
					); break;
		}
		return $gold;
	}
	/**
	 * 连续登录礼包
	 */
	public static function getLoginUser($day){
		$award = array();
		
		switch ($day) {
			case 1 : $award = array(
					'award' => array('gold' => 10000)
					); break;
			case 2 : $award = array(
					'award' => array('material' => array(0 , 5))
					); break;
			case 3 : $award = array(
					'award' => array('check' => array(0 , 5))
					); break;
			case 4 : $award = array(
					'award' => array('recruit' => array(1 , 5))
					); break;
			case 5 : $award = array(
					'award' => array('key' => array(4 , 2))
					); break;
			case 6 : $award = array(
					'award' => array('training' => array(2 , 3))
					); break;
			case 7 : $award = array(
					'award' => array('training' => array(3 , 2))
					); break;
		}
		return $award;
	}
	/**
	 * 城池税收
	 */
	public static function getCityUser($lv){
		$gold = 0;
		
		switch ($lv) {
			case 1 : $gold = 2000; break;
			case 2 : $gold = 2500; break;
			case 3 : $gold = 2500; break;
			case 4 : $gold = 6000; break;
			case 5 : $gold = 6000; break;
			case 6 : $gold = 6000; break;
			case 7 : $gold = 6000; break;
			case 8 : $gold = 7000; break;
			case 9 : $gold = 7000; break;
			case 10 : $gold = 7000; break;
			case 11 : $gold = 7000; break;
			case 12 : $gold = 7000; break;
			case 13 : $gold = 7000; break;
			case 14 : $gold = 7000; break;
			case 15 : $gold = 8000; break;
			case 16 : $gold = 8000; break;
			case 17 : $gold = 8000; break;
			case 18 : $gold = 8000; break;
			case 19 : $gold = 8000; break;
			case 20 : $gold = 8000; break;
			case 21 : $gold = 8000; break;
			case 22 : $gold = 8000; break;
			case 23 : $gold = 9000; break;
			case 24 : $gold = 9000; break;
			case 25 : $gold = 9000; break;
			case 26 : $gold = 9000; break;
			case 27 : $gold = 9000; break;
			case 28 : $gold = 9000; break;
			case 29 : $gold = 9000; break;
			case 30 : $gold = 15000; break;
			case 31 : $gold = 15000; break;
			case 32 : $gold = 15000; break;
			case 33 : $gold = 15000; break;
			case 34 : $gold = 15000; break;
			case 35 : $gold = 15000; break;
			case 36 : $gold = 16500; break;
			case 37 : $gold = 16500; break;
			case 38 : $gold = 16500; break;
			case 39 : $gold = 16500; break;
			case 40 : $gold = 16500; break;
			case 41 : $gold = 18000; break;
			case 42 : $gold = 18000; break;
			case 43 : $gold = 18000; break;
			case 44 : $gold = 18000; break;
			case 45 : $gold = 18000; break;
			case 46 : $gold = 19500; break;
			case 47 : $gold = 19500; break;
			case 48 : $gold = 19500; break;
			case 49 : $gold = 19500; break;
			case 50 : $gold = 19500; break;
			case 51 : $gold = 28000; break;
			case 52 : $gold = 28000; break;
			case 53 : $gold = 28000; break;
			case 54 : $gold = 28000; break;
			case 55 : $gold = 28000; break;
			case 56 : $gold = 30000; break;
			case 57 : $gold = 30000; break;
			case 58 : $gold = 30000; break;
			case 59 : $gold = 30000; break;
			case 60 : $gold = 30000; break;
			case 61 : $gold = 32000; break;
			case 62 : $gold = 32000; break;
			case 63 : $gold = 32000; break;
			case 64 : $gold = 32000; break;
			case 65 : $gold = 32000; break;
			case 66 : $gold = 34000; break;
			case 67 : $gold = 34000; break;
			case 68 : $gold = 34000; break;
			case 69 : $gold = 34000; break;
			case 70 : $gold = 34000; break;
			case 71 : $gold = 45000; break;
			case 72 : $gold = 45000; break;
			case 73 : $gold = 45000; break;
			case 74 : $gold = 45000; break;
			case 75 : $gold = 45000; break;
			case 76 : $gold = 0; break;
		}
		return $gold;
	}
	/**
	 * 占领城池税收
	 */
	public static function getCaptureCity($lv){
		$gold = 0;
		
		switch ($lv) {
			case 1 : $gold = 200; break;
			case 2 : $gold = 240; break;
			case 3 : $gold = 240; break;
			case 4 : $gold = 260; break;
			case 5 : $gold = 260; break;
			case 6 : $gold = 260; break;
			case 7 : $gold = 260; break;
			case 8 : $gold = 280; break;
			case 9 : $gold = 280; break;
			case 10 : $gold = 280; break;
			case 11 : $gold = 280; break;
			case 12 : $gold = 280; break;
			case 13 : $gold = 280; break;
			case 14 : $gold = 280; break;
			case 15 : $gold = 300; break;
			case 16 : $gold = 300; break;
			case 17 : $gold = 300; break;
			case 18 : $gold = 300; break;
			case 19 : $gold = 300; break;
			case 20 : $gold = 300; break;
			case 21 : $gold = 300; break;
			case 22 : $gold = 300; break;
			case 23 : $gold = 320; break;
			case 24 : $gold = 320; break;
			case 25 : $gold = 320; break;
			case 26 : $gold = 320; break;
			case 27 : $gold = 320; break;
			case 28 : $gold = 320; break;
			case 29 : $gold = 320; break;
			case 30 : $gold = 340; break;
			case 31 : $gold = 340; break;
			case 32 : $gold = 340; break;
			case 33 : $gold = 340; break;
			case 34 : $gold = 340; break;
			case 35 : $gold = 340; break;
			case 36 : $gold = 360; break;
			case 37 : $gold = 360; break;
			case 38 : $gold = 360; break;
			case 39 : $gold = 360; break;
			case 40 : $gold = 360; break;
			case 41 : $gold = 380; break;
			case 42 : $gold = 380; break;
			case 43 : $gold = 380; break;
			case 44 : $gold = 380; break;
			case 45 : $gold = 380; break;
			case 46 : $gold = 400; break;
			case 47 : $gold = 400; break;
			case 48 : $gold = 400; break;
			case 49 : $gold = 400; break;
			case 50 : $gold = 400; break;
			case 51 : $gold = 420; break;
			case 52 : $gold = 420; break;
			case 53 : $gold = 420; break;
			case 54 : $gold = 420; break;
			case 55 : $gold = 420; break;
			case 56 : $gold = 440; break;
			case 57 : $gold = 440; break;
			case 58 : $gold = 440; break;
			case 59 : $gold = 440; break;
			case 60 : $gold = 440; break;
			case 61 : $gold = 460; break;
			case 62 : $gold = 460; break;
			case 63 : $gold = 460; break;
			case 64 : $gold = 460; break;
			case 65 : $gold = 460; break;
			case 66 : $gold = 480; break;
			case 67 : $gold = 480; break;
			case 68 : $gold = 480; break;
			case 69 : $gold = 480; break;
			case 70 : $gold = 480; break;
			case 71 : $gold = 500; break;
			case 72 : $gold = 500; break;
			case 73 : $gold = 500; break;
			case 74 : $gold = 500; break;
			case 75 : $gold = 500; break;
			case 76 : $gold = 0; break;
		}
		return $gold;
	}
	/**
	 * 可招募将领
	 */
	public static function getGeneralsLv($lv){
		$count = 0;
		
		switch ($lv) {
			case 1 : $count = 3; break;
			case 2 : $count = 6; break;
			case 3 : $count = 6; break;
			case 4 : $count = 9; break;
			case 5 : $count = 9; break;
			case 6 : $count = 9; break;
			case 7 : $count = 9; break;
			case 8 : $count = 12; break;
			case 9 : $count = 12; break;
			case 10 : $count = 12; break;
			case 11 : $count = 12; break;
			case 12 : $count = 12; break;
			case 13 : $count = 12; break;
			case 14 : $count = 12; break;
			case 15 : $count = 15; break;
			case 16 : $count = 15; break;
			case 17 : $count = 15; break;
			case 18 : $count = 15; break;
			case 19 : $count = 15; break;
			case 20 : $count = 15; break;
			case 21 : $count = 15; break;
			case 22 : $count = 15; break;
			case 23 : $count = 18; break;
			case 24 : $count = 18; break;
			case 25 : $count = 18; break;
			case 26 : $count = 18; break;
			case 27 : $count = 18; break;
			case 28 : $count = 18; break;
			case 29 : $count = 18; break;
			case 30 : $count = 21; break;
			case 31 : $count = 21; break;
			case 32 : $count = 21; break;
			case 33 : $count = 21; break;
			case 34 : $count = 21; break;
			case 35 : $count = 21; break;
			case 36 : $count = 24; break;
			case 37 : $count = 24; break;
			case 38 : $count = 24; break;
			case 39 : $count = 24; break;
			case 40 : $count = 24; break;
			case 41 : $count = 27; break;
			case 42 : $count = 27; break;
			case 43 : $count = 27; break;
			case 44 : $count = 27; break;
			case 45 : $count = 27; break;
			case 46 : $count = 30; break;
			case 47 : $count = 30; break;
			case 48 : $count = 30; break;
			case 49 : $count = 30; break;
			case 50 : $count = 30; break;
			case 51 : $count = 33; break;
			case 52 : $count = 33; break;
			case 53 : $count = 33; break;
			case 54 : $count = 33; break;
			case 55 : $count = 33; break;
			case 56 : $count = 36; break;
			case 57 : $count = 36; break;
			case 58 : $count = 36; break;
			case 59 : $count = 36; break;
			case 60 : $count = 36; break;
			case 61 : $count = 39; break;
			case 62 : $count = 39; break;
			case 63 : $count = 39; break;
			case 64 : $count = 39; break;
			case 65 : $count = 39; break;
			case 66 : $count = 42; break;
			case 67 : $count = 42; break;
			case 68 : $count = 42; break;
			case 69 : $count = 42; break;
			case 70 : $count = 42; break;
			case 71 : $count = 45; break;
			case 72 : $count = 45; break;
			case 73 : $count = 45; break;
			case 74 : $count = 45; break;
			case 75 : $count = 45; break;
			case 76 : $count = 48; break;
		}
		return $count;
	}
	/**
	 * 将领可升最高等级
	 */
	public static function getGeneralsLevel($lv){
		$max_lv = 0;
		
		switch ($lv) {
			case 1 : $max_lv = 3; break;
			case 2 : $max_lv = 6; break;
			case 3 : $max_lv = 6; break;
			case 4 : $max_lv = 9; break;
			case 5 : $max_lv = 9; break;
			case 6 : $max_lv = 9; break;
			case 7 : $max_lv = 9; break;
			case 8 : $max_lv = 12; break;
			case 9 : $max_lv = 12; break;
			case 10 : $max_lv = 12; break;
			case 11 : $max_lv = 12; break;
			case 12 : $max_lv = 12; break;
			case 13 : $max_lv = 12; break;
			case 14 : $max_lv = 12; break;
			case 15 : $max_lv = 15; break;
			case 16 : $max_lv = 15; break;
			case 17 : $max_lv = 15; break;
			case 18 : $max_lv = 15; break;
			case 19 : $max_lv = 15; break;
			case 20 : $max_lv = 15; break;
			case 21 : $max_lv = 15; break;
			case 22 : $max_lv = 15; break;
			case 23 : $max_lv = 18; break;
			case 24 : $max_lv = 18; break;
			case 25 : $max_lv = 18; break;
			case 26 : $max_lv = 18; break;
			case 27 : $max_lv = 18; break;
			case 28 : $max_lv = 18; break;
			case 29 : $max_lv = 18; break;
			case 30 : $max_lv = 21; break;
			case 31 : $max_lv = 21; break;
			case 32 : $max_lv = 21; break;
			case 33 : $max_lv = 21; break;
			case 34 : $max_lv = 21; break;
			case 35 : $max_lv = 21; break;
			case 36 : $max_lv = 24; break;
			case 37 : $max_lv = 24; break;
			case 38 : $max_lv = 24; break;
			case 39 : $max_lv = 24; break;
			case 40 : $max_lv = 24; break;
			case 41 : $max_lv = 27; break;
			case 42 : $max_lv = 27; break;
			case 43 : $max_lv = 27; break;
			case 44 : $max_lv = 27; break;
			case 45 : $max_lv = 27; break;
			case 46 : $max_lv = 30; break;
			case 47 : $max_lv = 30; break;
			case 48 : $max_lv = 30; break;
			case 49 : $max_lv = 30; break;
			case 50 : $max_lv = 30; break;
			case 51 : $max_lv = 33; break;
			case 52 : $max_lv = 33; break;
			case 53 : $max_lv = 33; break;
			case 54 : $max_lv = 33; break;
			case 55 : $max_lv = 33; break;
			case 56 : $max_lv = 36; break;
			case 57 : $max_lv = 36; break;
			case 58 : $max_lv = 36; break;
			case 59 : $max_lv = 36; break;
			case 60 : $max_lv = 36; break;
			case 61 : $max_lv = 39; break;
			case 62 : $max_lv = 39; break;
			case 63 : $max_lv = 39; break;
			case 64 : $max_lv = 39; break;
			case 65 : $max_lv = 39; break;
			case 66 : $max_lv = 42; break;
			case 67 : $max_lv = 42; break;
			case 68 : $max_lv = 42; break;
			case 69 : $max_lv = 42; break;
			case 70 : $max_lv = 42; break;
			case 71 : $max_lv = 45; break;
			case 72 : $max_lv = 45; break;
			case 73 : $max_lv = 45; break;
			case 74 : $max_lv = 45; break;
			case 75 : $max_lv = 45; break;
			case 76 : $max_lv = 48; break;
		}
		return $max_lv;
	}
	/**
	 * 可攻占城池
	 */
	public static function getCityCount($lv){
		
		$count = 0;
		
		switch ($lv) {
		case 1 : $count = 3; break;
		case 2 : $count = 5; break;
		case 3 : $count = 5; break;
		case 4 : $count = 7; break;
		case 5 : $count = 7; break;
		case 6 : $count = 7; break;
		case 7 : $count = 7; break;
		case 8 : $count = 9; break;
		case 9 : $count = 9; break;
		case 10 : $count = 9; break;
		case 11 : $count = 9; break;
		case 12 : $count = 9; break;
		case 13 : $count = 9; break;
		case 14 : $count = 9; break;
		case 15 : $count = 11; break;
		case 16 : $count = 11; break;
		case 17 : $count = 11; break;
		case 18 : $count = 11; break;
		case 19 : $count = 11; break;
		case 20 : $count = 11; break;
		case 21 : $count = 11; break;
		case 22 : $count = 11; break;
		case 23 : $count = 13; break;
		case 24 : $count = 13; break;
		case 25 : $count = 13; break;
		case 26 : $count = 13; break;
		case 27 : $count = 13; break;
		case 28 : $count = 13; break;
		case 29 : $count = 13; break;
		case 30 : $count = 15; break;
		case 31 : $count = 15; break;
		case 32 : $count = 15; break;
		case 33 : $count = 15; break;
		case 34 : $count = 15; break;
		case 35 : $count = 15; break;
		case 36 : $count = 17; break;
		case 37 : $count = 17; break;
		case 38 : $count = 17; break;
		case 39 : $count = 17; break;
		case 40 : $count = 17; break;
		case 41 : $count = 19; break;
		case 42 : $count = 19; break;
		case 43 : $count = 19; break;
		case 44 : $count = 19; break;
		case 45 : $count = 19; break;
		case 46 : $count = 21; break;
		case 47 : $count = 21; break;
		case 48 : $count = 21; break;
		case 49 : $count = 21; break;
		case 50 : $count = 21; break;
		case 51 : $count = 23; break;
		case 52 : $count = 23; break;
		case 53 : $count = 23; break;
		case 54 : $count = 23; break;
		case 55 : $count = 23; break;
		case 56 : $count = 25; break;
		case 57 : $count = 25; break;
		case 58 : $count = 25; break;
		case 59 : $count = 25; break;
		case 60 : $count = 25; break;
		case 61 : $count = 27; break;
		case 62 : $count = 27; break;
		case 63 : $count = 27; break;
		case 64 : $count = 27; break;
		case 65 : $count = 27; break;
		case 66 : $count = 29; break;
		case 67 : $count = 29; break;
		case 68 : $count = 29; break;
		case 69 : $count = 29; break;
		case 70 : $count = 29; break;
		case 71 : $count = 31; break;
		case 72 : $count = 31; break;
		case 73 : $count = 31; break;
		case 74 : $count = 31; break;
		case 75 : $count = 31; break;
		case 76 : $count = 33; break;
		}
		return $count;
	}
	/**
	 * 战略取值
	 */
	public static function getStrategy($state){
		switch ($state) {
			case 0 : $num = 20;
					break;
			case 1 : $num = 50;
					break;
			case 2 : $num = 80;
					break;
			case 3 : $num = 100;
					break;
		}
		return $num;
	}
	/**
	 * 将领等级可带兵数
	 */
	public static function getGeneralsBarracksLv($lv){
		$count = array();
		
		switch ($lv) {
			case 1 : $count = array(
					'count' => 500,
					); break;
			case 2 : $count = array(
					'count' => 600,
					); break;
			case 3 : $count = array(
					'count' => 700,
					); break;
			case 4 : $count = array(
					'count' => 800,
					); break;
			case 5 : $count = array(
					'count' => 900,
					); break;
			case 6 : $count = array(
					'count' => 1100,
					); break;
			case 7 : $count = array(
					'count' => 1300,
					); break;	
			case 8 : $count = array(
					'count' => 1500,
					); break;
			case 9 : $count = array(
					'count' => 1800,
					); break;
			case 10 : $count = array(
					'count' => 2100,
					); break;
			case 11 : $count = array(
					'count' => 2400,
					); break;
			case 12 : $count = array(
					'count' => 2700,
					); break;
			case 13 : $count = array(
					'count' => 3100,
					); break;
			case 14 : $count = array(
					'count' => 3500,
					); break;
			case 15 : $count = array(
					'count' => 3900,
					); break;		
			case 16 : $count = array(
					'count' => 4400,
					); break;
			case 17 : $count = array(
					'count' => 4900,
					); break;
			case 18 : $count = array(
					'count' => 5400,
					); break;
			case 19 : $count = array(
					'count' => 6000,
					); break;
			case 20 : $count = array(
					'count' => 6600,
					); break;
			case 21 : $count = array(
					'count' => 7200,
					); break;	
			case 22 : $count = array(
					'count' => 7800,
					); break;
			case 23 : $count = array(
					'count' => 8500,
					); break;
			case 24 : $count = array(
					'count' => 9200,
					); break;
			case 25 : $count = array(
					'count' => 10000,
					); break;
			case 26 : $count = array(
					'count' => 10800,
					); break;
			case 27 : $count = array(
					'count' => 11500,
					); break;	
			case 28 : $count = array(
					'count' => 12300,
					); break;
			case 29 : $count = array(
					'count' => 13200,
					); break;
			case 30 : $count = array(
					'count' => 14100,
					); break;
			case 31 : $count = array(
					'count' => 15000,
					); break;
			case 32 : $count = array(
					'count' => 15900,
					); break;
			case 33 : $count = array(
					'count' => 16900,
					); break;												
		}
		return $count;
	}
	/**
	 * 等级排序 大到小
	 * @param unknown_type $arr
	 */
	public static function getArrSort($arr){
	   $i=0 ;
	   $j=0 ;
	   $temp=0 ;
	   $count = count($arr);
	   for($i=0; $i < $count; $i++){
	     for($j=$i; $j < $count; $j++){
	     	if($arr[$i]['Value']['lv'] < $arr[$j]['Value']['lv']){
		        $temp=$arr[$i];
		        $arr[$i]=$arr[$j];
		        $arr[$j]=$temp;
	      	}
	     }
	   }
	//   print_r($arr);
	  return $arr;
	}
	/**
	 * 按攻城时间从小到大
	 * @param unknown_type $arr
	 */
	public static function getGeneralsTime($arr){
	   $i=0 ;
	   $j=0 ;
	   $temp=0 ;
	   $count = count($arr);
	   for($i=0; $i < $count; $i++){
	     for($j=$i; $j < $count; $j++){
	     	if($arr[$i] > $arr[$j]){
		        $temp=$arr[$i];
		        $arr[$i]=$arr[$j];
		        $arr[$j]=$temp;
	      	}
	     }
	   }
	//   print_r($arr);
	  return $arr;
	}
	/**
	 * 武将等级排序 大到小
	 * @param unknown_type $arr
	 */
	public static function getGeneralsArrSort($arr){
	   $i=0 ;
	   $j=0 ;
	   $temp=0 ;
	   $count = count($arr);
	   for($i=0; $i < $count; $i++){
	     for($j=$i; $j < $count; $j++){
	     	if($arr[$i]['lv'] < $arr[$j]['lv']){
		        $temp=$arr[$i];
		        $arr[$i]=$arr[$j];
		        $arr[$j]=$temp;
	      	}
	     }
	  }
	//   print_r($arr);
	  return $arr;
	}
	/**
	 * 材料排序
	 */
	public static function getMaterial($arr){
		$i=0 ;
	    $j=0 ;
	    $temp=0 ;
	    $count = count($arr);
	    for($i=0; $i < $count; $i++){
	    	for($j=$i; $j < $count; $j++){
		    	if($i < $j){
		        	$temp=$arr[$i];
		        	$arr[$i]=$arr[$j];
		        	$arr[$j]=$temp;
		     	}
	    	}
	  	}
	   return $arr;
	} 
	/**
	 * 武将攻击输赢
	 * @param $uin
	 * @param $ap
	 * @param $hp
	 * @param $is_ap
	 * @param $is_hp
	 */
	public static function getAttackGeneral($skills , $ap , $hp , $d , $is_ap , $is_hp , $is_d , $is_skills){
		$i = 2;  //选择谁先打
		$go = 0;
		loadLib('App_Overlord_Collocate');
		$data = App_Overlord_Collocate::getSkill();
		
	// ================================A方武将技能 =====================================
		if(!empty($skills['special'])){
			foreach ($skills['special'] as $k => $row){
				//判断是否有闭月斩     注：30%的可能在攻击将领时使对方将领在本场战斗中无法使用任何主动技能
				if ($k == 'closed'){
					$a_skill_ap[$k]['name'] =  $data['special'][$k][$row]['name'];
					$a_skill_ap[$k]['odds'] =  $data['special'][$k][$row]['odds'];
					$a_skill_ap[$k]['num'] =  $data['special'][$k][$row]['num'];
				}elseif ($k == 'sharpness'){
				//判断是否有武将属性下降技能   洗尽锋芒 ：可使对方将领当时最强的属性在本场战斗中降低8%
					//C数组拼成一维数组
					$c_arr = array($is_ap , $is_hp , $is_d);
					$c_max = array_search(max($c_arr) , $c_arr);
					$c_num = $data['special'][$k][$row]['num'];
					//C武将最强的属性降低8%
					if ($c_max == 0){
						$is_ap -= intval($is_ap * $c_num);
						$is_name = '攻击力';
					}elseif ($c_max == 1){
						$is_hp -= intval($is_hp * $c_num);
						$is_name = '生命值';
					}else{
						$is_d -= intval($is_d * $c_num);
						$is_name = '防御力';
					}
					//特殊技能记录
					$a_skill_special = array($data['special'][$k][$row]['name'] , $c_num , $is_name);
				}
			}
		}
		
		
		if (!empty($skills['study'])){
			foreach ($skills['study'] as $k => $row){
				//判断是否有武将附带伤害
				if ($k == 'general_ap' || $k == 'general_ap_1'){
					$a_skill_ap[$k]['name'] =  $data['study'][$k][$row]['name'];
					$a_skill_ap[$k]['odds'] =  $data['study'][$k][$row]['odds'];
					$a_skill_ap[$k]['num'] =  $data['study'][$k][$row]['num'];
				}elseif ($k == 'general_defense' || $k == 'general_defense_1'){
					//A武将的防御技能
					$a_skill_defense[$k]['name'] =  $data['study'][$k][$row]['name'];
					$a_skill_defense[$k]['odds'] =  $data['study'][$k][$row]['odds'];
					$a_skill_defense[$k]['num'] =  $data['study'][$k][$row]['num'];
				}
			}
		}else{
			$skill = -1; 
		}
		if (empty($a_skill_ap)){
			$a_skill_ap = -1;
		}
		if (empty($a_skill_defense)){
			$a_skill_defense = -1;
		}
	// ==================================C方武将技能	==============================
		if(!empty($is_skills['special'])){
			foreach ($is_skills['special'] as $k => $row){
				//判断是否有闭月斩     注：30%的可能在攻击将领时使对方将领在本场战斗中无法使用任何主动技能
				if ($k == 'closed'){
					$c_skill_ap[$k]['name'] =  $data['special'][$k][$row]['name'];
					$c_skill_ap[$k]['odds'] =  $data['special'][$k][$row]['odds'];
					$c_skill_ap[$k]['num'] =  $data['special'][$k][$row]['num'];
				}elseif ($k == 'sharpness'){
				//判断是否有武将属性下降技能   洗尽锋芒 ：可使对方将领当时最强的属性在本场战斗中降低8%
					//C数组拼成一维数组
					$a_arr = array($ap , $hp , $d);
			
					$a_max = array_search(max($a_arr) , $a_arr);
			
					$a_num = $data['special'][$k][$row]['num'];
			
					//C武将最强的属性降低8%
					if ($a_max == 0){
						$ap -= intval($ap * $a_num);
						$name = '攻击力';
					}elseif ($a_max == 1){
						$hp -= intval($hp * $a_num);
						$name = '生命值';
					}else{
						$d -= intval($d * $a_num);
						$name = '防御力';
					}
					//特殊技能记录
					$c_skill_special = array($data['special'][$k][$row]['name'] , $a_num , $name);
				}
			}
		}
		if (!empty($is_skills['study'])){
			foreach ($is_skills['study'] as $k => $row){
				//判断是否有武将附带伤害
				if ($k == 'general_ap' || $k == 'general_ap_1'){
					$c_skill_ap[$k]['name'] =  $data['study'][$k][$row]['name'];
					$c_skill_ap[$k]['odds'] =  $data['study'][$k][$row]['odds'];
					$c_skill_ap[$k]['num'] =  $data['study'][$k][$row]['num'];
				}elseif ($k == 'general_defense' || $k == 'general_defense_1'){
					//C武将的防御技能
					$c_skill_defense[$k]['name'] =  $data['study'][$k][$row]['name'];
					$c_skill_defense[$k]['odds'] =  $data['study'][$k][$row]['odds'];
					$c_skill_defense[$k]['num'] =  $data['study'][$k][$row]['num'];
				}
			}
		}else{
			$is_skill = -1; 
		}
		if (empty($c_skill_ap)){
			$c_skill_ap = -1;
		}
		if (empty($c_skill_defense)){
			$c_skill_defense = -1;
		}
		while (1){
			if ($i == 1){
				$rand_ap = self::getRandAttack($ap,$is_d);
			// ==================================a武将的攻击技能随机=======================	
				//判断A武将有没有攻击技能
				if (empty($skill)){
					//A没中闭月斩 ，技能可用
					if (empty(self::$a_special)){
						if ($a_skill_ap != -1){
							//随机一个攻击技能
							$a_skill_rand_type = array_rand($a_skill_ap);
				
							//看武将攻击技能概率
							$a_skill_ap_rand = self::getChance($a_skill_ap[$a_skill_rand_type]['odds']);
							
							//没有触发闭月斩 
							if ($a_skill_rand_type != 'closed' && empty($a_skill_ap_rand)){
								if (empty($is_skill) && $c_skill_defense != -1){
									//随机C武将的防御技能
									$c_skill_rand_type = array_rand($c_skill_defense , 1);
									//看C武将技能概率
									$c_skill_defense_rand = self::getChance($c_skill_defense[$c_skill_rand_type]['odds']);
								}
							}elseif ($a_skill_rand_type == 'closed' && !empty($a_skill_ap_rand)){
							//触发闭月斩 	
								self::$c_special = 1;
							}
						}
					}
				}else{
					$skill_studio_ap[] = $skill;
				}
			// ==================================c武将的防御技能触发=======================	
			
				if ($c_skill_defense != -1 && !empty($c_skill_rand)){
					//如果C方武将有防御技能并触发
					$is_skill_studio_defense[] = $c_skill_defense[$c_skill_rand_type]['name'];
				
					if ($c_skill_defense[$c_skill_rand_type]['num'] == 1){
						$rand_ap = 1;
					}else{
						$rand_ap -= intval($rand_ap * $c_skill_defense[$c_skill_rand_type]['num'] / 100);
					}
					$skill_studio_ap[] = -1;
				}else{
			// ==================================c武将的攻击技能触发=======================
			
					//a武将使用攻击技能，触发失败
					if (empty($a_skill_ap_rand)){
						$skill_studio_ap[] = -1;
					}else{
						//a没中闭月斩 ，技能可用
						if (empty(self::$a_special)){
							$rand_ap += intval($rand_ap * $a_skill_ap[$a_skill_rand_type]['num'] / 100);
							//技能赋值
							$skill_studio_ap[] = $a_skill_ap[$a_skill_rand_type]['name'];
						}else{
							$skill_studio_ap[] = -1;
						}
					}
					//防御技能没触发、赋值为-1
					$is_skill_studio_defense[] = -1;
				}
				//减少血量
				$is_hp -= $rand_ap;
				if ($is_hp < 0){
					$is_hp = 0;
				}
				$studio_ap[] = $rand_ap;
				
				$i = 2;
			}else{
				$rand_is_ap= self::getRandAttack($is_ap,$d);
			// ==================================c武将的攻击技能随机=======================
			
			//判断C武将有没有攻击技能
				if (empty($is_skill)){
					//C没中闭月斩 ，技能可用
					if (empty(self::$c_special)){
						if ($c_skill_ap != -1){
							//随机C武将一个攻击技能
							$c_skill_rand_type = array_rand($c_skill_ap);
							//看c武将技能概率
							$c_skill_ap_rand = self::getChance($c_skill_ap[$c_skill_rand_type]['odds']);
						
						//没有触发闭月斩 
							if ($c_skill_rand_type != 'closed' && empty($c_skill_ap_rand)){
								
								if (empty($skill) && $a_skill_defense != -1){
									//随机a武将的防御技能
									$a_skill_rand_type = array_rand($a_skill_defense);
									//看武a将技能概率
									$a_skill_defense_rand = self::getChance($a_skill_defense[$a_skill_rand_type]['odds']);
								}
							}elseif ($c_skill_rand_type == 'closed' && !empty($c_skill_ap_rand)){
							//触发闭月斩 	
								self::$a_special = 1;
							}
						}
					}
				}else{
					$skill_studio_ap[] = $is_skill;
				}
			// ==================================a武将的防御触发=======================
			
				if (!empty($a_skill_defense_rand)){
					//如果a方武将有防御技能并触发
					$skill_studio_defense[] = $a_skill_defense[$a_skill_rand_type]['name'];
					if ($a_skill_defense[$a_skill_rand_type]['num'] == 1){
						$rand_is_ap = 1;
					}else{
						$rand_is_ap -= intval($rand_ap * $a_skill_defense[$a_skill_rand_type]['num'] / 100);
					}
				}else{
			// ================================ c武将武技触发 ============================
			
					//c武将使用攻击技能，触发失败
					if (empty($c_skill_ap_rand)){
						$is_skill_studio_ap[] = -1;
					}else{
						//c没中武将闭月斩，技能可用
						if (empty(self::$c_special)){
							$rand_is_ap += intval($rand_is_ap * $c_skill_ap[$c_skill_rand_type]['num'] / 100);
							//技能赋值
							$is_skill_studio_ap[] = $c_skill_ap[$c_skill_rand_type]['name'];
						}else{
							$is_skill_studio_ap[] = -1;
						}
					}
					//防御技能没触发、赋值为-1
					$skill_studio_defense[] = -1;
				
				}
				
				$hp -= $rand_is_ap;
				if ($hp < 0){
					$hp = 0;
				}
				$is_studio_ap[] = $rand_is_ap;
				$i = 1;
			}
			//血量满足或者打5回合结束
			if ($hp <= 0 || $is_hp <= 0 || $go >= 11){break;};
			$go++;
		}
		if (empty($studio_ap)){
			$studio_ap[] = 0;
		}
		if (empty($is_studio_ap)){
			$is_studio_ap[] = 0;
		}
		if (empty($skill_studio_ap)){
			$skill_studio_ap[] = -1;
		}
		if (empty($skill_studio_defense)){
			$skill_studio_defense[] = -1;
		}
		if (empty($is_skill_studio_ap)){
			$is_skill_studio_ap[] = -1;
		}
		if (empty($is_skill_studio_defense)){
			$is_skill_studio_defense[] = -1;
		}
		if (empty($a_skill_special)){
			$a_skill_special = -1;
		}
		if (empty($c_skill_special)){
			$c_skill_special = -1;
		}
		return $result=array('hp' => $hp , 'is_hp' => $is_hp , 'studio_ap' => $studio_ap , 'skill_studio_defense' => $skill_studio_defense , 'is_studio_ap' => $is_studio_ap , 'skill_studio_ap' => $skill_studio_ap, 'is_skill_studio_ap' => $is_skill_studio_ap , 'is_skill_studio_defense' => $is_skill_studio_defense , 'a_skill_special' => $a_skill_special , 'c_skill_special' => $c_skill_special);
	}
	/**
	 * 随机攻击力
	 * @param $ap
	 */
	public static function getRandAttack($a,$d){
		$ap = $a - $d;
		if ($ap <= 0){
			$ap = 5;
		}
		$ap += rand(-2,2);
		return $ap;
	}
	/**
	 * 兵攻击
	 * A VS B
	 * $state 1 A方武将胜利
	 * $state 2 B方武将胜利
	 */
	public static function getAttackbarracks($k , $skills , $is_skills , $a , $b , $state_barracks , $is_skills_barracks , $a_skill_special , $b_skill_special , $tactic , $is_tactic){
		$suma = array_sum($a);
		$sumb = array_sum($b);
		//根据战略
		$a_tactic = self::getStrategy($tactic);
		$b_tactic = self::getStrategy($is_tactic);
		$a_tactic_sum = intval($suma * (1 - $a_tactic / 100));
		$b_tactic_sum = intval($sumb * (1 - $b_tactic / 100));
		$attack = false;
		while(1){
			if($attack == false){//第一次攻击
				if($a[2] != 0){//判断是否有弓兵
					if ($b[2] != 0){
						//采集攻击数据
						$b_num = self::getAttackCount($a[2] , $b[2]);
						//A有弓箭攻击技能，加伤害
						if (!empty($skills['toxotae_ap'])){
							$b_num -= intval(($b[2] - $b_num) * $skills['toxotae_ap']['num'] / 100);
						}
						//将领战斗胜利士兵加属性技能
						if (!empty($skills['defeat'])){
							$b_num -= intval(($b[2] - $b_num) * $skills['defeat']['num'] / 100);
						}
						//麾下部队在攻城时攻击力提高
						if (!empty($skills['carry'])){
							$b_num -= intval(($b[2] - $b_num) * $skills['carry']['num'] / 100);
						}
						//麾下部队在驻防时攻击力提高
						if (!empty($skills['against'])){
							$b_num -= intval(($b[2] - $b_num) * $skills['against']['num'] / 100);
						}
						//B有弓箭防御技能
						if (!empty($is_skills['toxotae_defense'])){
							$b_num += intval(($b[2] - $b_num) * $is_skills['toxotae_defense']['num'] / 100);
						}
						//将领战斗失败士兵加属性技能
						if (!empty($is_skills['break'])){
							$b_num += intval(($b[2] - $b_num) * $is_skills['break']['num'] / 100);
						}
						//麾下部队在攻城时防御力
						if (!empty($is_skills['consolidate'])){
							$b_num += intval(($b[2] - $b_num) * $is_skills['consolidate']['num'] / 100);
						}
						//麾下部队在驻防时防御力
						if (!empty($is_skills['defend'])){
							$b_num += intval(($b[2] - $b_num) * $is_skills['defend']['num'] / 100);
						}
						$b[2] = $b_num;
					}else{
						foreach ($b as $f => $row){
							if($row != 0){
								$b[$f] = self::getAttackCount($a[2] , $row);
								if ($f == 0){
									$b[$f] += intval(($row - $a[$f]) * 0.1);   //打步兵,步兵克弓箭，伤害减少10%
								}elseif ($f == 1){
									$b[$f] -= intval(($row - $a[$f]) * 0.1);  //打骑兵,弓箭克骑兵，伤害添加10%
								}
								break;
							}
						}
					}
				}
				if (!empty($a)){
					$is_count['a'][] = $b;
					//打兵技能记录
					$is_count['a_barracks'][] = 0;
					$is_count['a_special'][] = 0;
				}
				//打一次判断一次
				$suma=array_sum($a);
				$sumb=array_sum($b);//计算双方剩余兵力
				if ($suma <= $a_tactic_sum){
					$state = 2;
					break;
				}
				if ($sumb <= $b_tactic_sum){
					$state = 1;
					break;
				}
				if($b[2] != 0){//判断是否有弓兵
					if ($a[2] != 0){
						$a_num = self::getAttackCount($b[2] , $a[2]);
						//B有弓箭攻击技能，加伤害
						if (!empty($is_skills['toxotae_ap'])){
							$a_num -= intval(($a[2] - $a_num) * $is_skills['toxotae_ap']['num'] / 100);
						}
						//将领战斗失败士兵加属性技能
						if (!empty($is_skills['break'])){
							$a_num -= intval(($a[2] - $a_num) * $is_skills['break']['num'] / 100);
						}
						//麾下部队在攻城时攻击力提高
						if (!empty($is_skills['carry'])){
							$a_num -= intval(($a[2] - $a_num) * $is_skills['carry']['num'] / 100);
						}
						//麾下部队在驻防时攻击力提高
						if (!empty($is_skills['against'])){
							$a_num -= intval(($a[2] - $a_num) * $is_skills['against']['num'] / 100);
						}
						//A有弓箭防御技能
						if (!empty($skills['toxotae_defense'])){
							$a_num += intval(($a[2] - $a_num) * $skills['toxotae_defense']['num'] / 100);
						}
						//将领战斗胜利士兵加属性技能
						if (!empty($skills['defeat'])){
							$a_num += intval(($a[2] - $a_num) * $skills['defeat']['num'] / 100);
						}
						//麾下部队在攻城时防御力
						if (!empty($skills['consolidate'])){
							$a_num += intval(($a[2] - $a_num) * $skills['consolidate']['num'] / 100);
						}
						//麾下部队在驻防时防御力
						if (!empty($skills['defend'])){
							$a_num += intval(($a[2] - $a_num) * $skills['defend']['num'] / 100);
						}
						$a[2] = $a_num;
					}else{
						foreach ($a as $f => $is_row){
							if($is_row != 0){
								$a[$f] = self::getAttackCount($a[2] , $is_row);
								if ($f == 0){
									$a[$f] +=  intval(($is_row - $a[$f]) * 0.1);   //打步兵,步兵克弓箭，伤害减少10%
								}elseif ($f == 1){
									$a[$f] -=  intval(($is_row - $a[$f]) * 0.1);   //打骑兵,弓箭克骑兵，伤害添加10%
								}
								break;
							}
						}
					}
				}
				
				if (!empty($b)){
					$is_count['b'][] = $a;
					//打兵技能记录
					$is_count['b_barracks'][] = 0;
					$is_count['b_special'][] = 0;
				}
				$attack = true;
			}else{
				//打一次判断一次
				$suma=array_sum($a);
				$sumb=array_sum($b);//计算双方剩余兵力
				if ($suma <= $a_tactic_sum){
					$state = 2;
					break;
				}
				if ($sumb <= $b_tactic_sum){
					$state = 1;
					break;
				}
				//第二次A攻击
				$b = self::getAorB($skills , $is_skills , $a , $b);
				if (!empty($state_barracks)){
					$a_barracks_type = array_rand($state_barracks);
					//看武将攻击士兵技能概率
					$a_barracks_rand = self::getChance($state_barracks[$a_barracks_type]['odds']);
					if (!empty($a_barracks_rand)){
						if (!empty($b)){
							//随机一种兵打
							$a_rand = array_rand($b);
							$b_study_barracks = intval($b[$a_rand] * $state_barracks[$a_barracks_type]['num'] / 100);
							$b[$a_rand] -= $b_study_barracks;
						}
						$is_count['a_barracks'][] = array($a_rand , $b_study_barracks , $state_barracks[$a_barracks_type]['name']);
					}else{
						$is_count['a_barracks'][] = 0;
					}
				}else{
					$is_count['a_barracks'][] = 0;
				}
				//打兵技能只能出现一种
				if (!empty($a_skill_special) && empty($a_barracks_rand)){
					$a_special_type = array_rand($a_skill_special);
					//看武将攻击士兵技能概率,特殊打兵技能
					$a_special_rand = self::getChance($a_skill_special[$a_special_type]['odds']);
					if (!empty($a_special_rand)){
				
						if (!empty($b)){
							//循环打兵
							foreach ($b as $g => $k){
								$a_barracks[$g] = intval($k * $a_skill_special[$a_special_type]['num'] / 100);
								$b[$g] -= $a_barracks[$g];
							}
							$is_count['a_special'][] = array($a_barracks , $a_skill_special[$a_special_type]['name']);
						}else{
							$is_count['a_special'][] = 0;
						}
					}else{
						$is_count['a_special'][] = 0;
					}
				}else{
					$is_count['a_special'][] = 0;
				}
				if (!empty($b)){
					$is_count['a'][] = $b;
				}
				//打一次判断一次
				$suma=array_sum($a);
				$sumb=array_sum($b);//计算双方剩余兵力
				if ($suma <= $a_tactic_sum){
					$state = 2;
					break;
				}
				if ($sumb <= $b_tactic_sum){
					$state = 1;
					break;
				}
				//第二次攻击b先攻击
				$a = self::getAorB($is_skills , $skills , $b , $a);
				if (!empty($is_skills_barracks)){
					$b_barracks_type = array_rand($is_skills_barracks);
					//看武将攻击士兵技能概率
					$b_barracks_rand = self::getChance($is_skills_barracks[$b_barracks_type]['odds']);
					if (!empty($b_barracks_rand)){
			
						if (!empty($a)){
							//随机一种兵打
							$b_rand = array_rand($a);
							$a_study_barracks = intval($a[$b_rand] * $is_skills_barracks[$b_barracks_type]['num'] / 100);
							$a[$b_rand] -= $a_study_barracks;
						}
						
						$is_count['b_barracks'][] = array($b_rand , $a_study_barracks , $is_skills_barracks[$b_barracks_type]['name']);
					}else{
						$is_count['b_barracks'][] = 0;
					}
				}else{
					$is_count['b_barracks'][] = 0;
				}
				if (!empty($b_skill_special)){
					$b_special_type = array_rand($b_skill_special);
					//看武将攻击士兵技能概率,特殊打兵技能
					$b_special_rand = self::getChance($b_skill_special[$b_special_type]['odds']);
					if (!empty($b_special_rand)){
						if (!empty($a)){
							//循环打兵
							foreach ($a as $g => $k){
								$b_barracks[$g] = intval($a[$g] * $b_skill_special[$b_special_type]['num'] / 100);
								$a[$g] -= $b_barracks[$g];
							}
							$is_count['b_special'][] = array($b_barracks , $b_skill_special[$b_special_type]['name']);
						}else{
							$is_count['b_special'][] = 0;
						}
					}else{
						$is_count['b_special'][] = 0;
					}
				}else{
					$is_count['b_special'][] = 0;
				}
				if (!empty($a)){
					$is_count['b'][] = $a;
				}
			}
			//打一次判断一次
			$suma=array_sum($a);
			$sumb=array_sum($b);//计算双方剩余兵力
			if ($suma <= $a_tactic_sum){
				$state = 2;
				break;
			}
			if ($sumb <= $b_tactic_sum){
				$state = 1;
				break;
			}
		}
	
		$pk = array($a, $b, $is_count , $state);
	
		return $pk;
	}
	/**
	 * 兵的攻击数量
	 * @param $ap
	 */
	public static function getAttackCount($a , $b){
		$ap = intval(0.3 * $a);
		if (empty($ap)){
			$ap = 1;
		}
		$count = $b -$ap;
		if ($count < 0){
			$count = 0;
		}
		return $count;
	}
	/**
	 * 兵种第二次攻击
	 */
	public static function getAorB($skills , $is_skills , $a , $b){
		$suma = array_sum($a);
		$a_num = array();
		$b_num = array();
		//第二次攻击a先攻击
		for($i = 0; $i < 4; $i++){
			if($suma == 0){
				break;
			}else{
				if ($a[$i] != 0){
					if($b[$i] != 0){//判断对方位置是否有兵
						$b_num[$i] = self::getAttackCount($a[$i] , $b[$i]);
				
						if ($i == 2){
				
							//A有弓箭攻击技能，加伤害
							if (!empty($skills['toxotae_ap'])){
							
								$b_num[$i] -= intval(($b[$i] - $b_num[$i]) * $skills['toxotae_ap']['num'] / 100);
							
							}
							//B有弓箭防御技能
							if (!empty($is_skills['toxotae_defense'])){
								$b_num[$i] += intval(($b[$i] - $b_num[$i]) * $is_skills['toxotae_defense']['num'] / 100);
							}
						}
						if ($i == 0){
							//A有步兵攻击技能，加伤害
							if (!empty($skills['infantry_ap'])){
								$b_num[$i] -= intval(($b[$i] - $b_num[$i]) * $skills['infantry_ap']['num'] / 100);
							}
							//B有步兵防御技能
							if (!empty($is_skills['infantry_defense'])){
								$b_num[$i] -= intval(($b[$i] - $b_num[$i]) * $is_skills['infantry_ap']['num'] / 100);
							}
						}
						if ($i == 1){
							//A有骑兵攻击技能，加伤害
							if (!empty($skills['cavalryman_ap'])){
								$b_num[$i] -= intval(($b[$i] - $b_num[$i]) * $skills['cavalryman_ap']['num'] / 100);
							}
							//B有骑兵防御技能
							if (!empty($is_skills['cavalryman_defense'])){
								$b_num[$i] -= intval(($b[$i] - $b_num[$i]) * $is_skills['cavalryman_ap']['num'] / 100);
							}
						}
						if ($i == 3){
							//A有特种攻击技能，加伤害
							if (!empty($skills['special_ap'])){
								$b_num[$i] -= intval(($b[$i] - $b_num[$i]) * $skills['special_ap']['num'] / 100);
							}
							//B有特种防御技能
							if (!empty($is_skills['special_defense'])){
								$b_num[$i] -= intval(($b[$i] - $b_num[$i]) * $is_skills['special_ap']['num'] / 100);
							}
						}
						//将领战斗胜利士兵加属性技能
						if (!empty($skills['defeat'])){
							$b_num[$i] -= intval(($b[$i] - $b_num[$i]) * $skills['defeat']['num'] / 100);
						}
						//麾下部队在攻城时攻击力提高
						if (!empty($skills['carry'])){
							$b_num[$i] -= intval(($b[2] - $b_num[$i]) * $skills['carry']['num'] / 100);
						}
						//麾下部队在驻防时攻击力提高
						if (!empty($skills['against'])){
							$b_num[$i] -= intval(($b[2] - $b_num[$i]) * $skills['against']['num'] / 100);
						}
						//将领战斗失败士兵加属性技能
						if (!empty($is_skills['break'])){
							$b_num[$i] += intval(($b[$i] - $b_num[$i]) * $is_skills['break']['num'] / 100);
						}
						//麾下部队在攻城时防御力
						if (!empty($is_skills['consolidate'])){
							$b_num[$i] += intval(($a[2] - $b_num[$i]) * $is_skills['consolidate']['num'] / 100);
						}
						//麾下部队在驻防时防御力
						if (!empty($is_skills['defend'])){
							$b_num[$i] += intval(($a[2] - $b_num[$i]) * $is_skills['defend']['num'] / 100);
						}
						$b[$i] = $b_num[$i];
					}else{
						foreach ($b as $f => $row){
							if($row != 0){
								$b[$f] = self::getAttackCount($a[$i] , $row);
								break;
							}
						}
					}	
				}			
			}		
		}
		return $b;
	}
	/**
	 * 等级经验列表
	 */
	public static function getExp($level){
		$guide = array();
		
		switch ($level) {
			case 1: $guide = array( 'staff' => 2 , 'upgrade' => 100 ); break;
			case 2: $guide = array( 'staff' => 3 , 'upgrade' => 75 ); break;
			case 3: $guide = array( 'staff' => 4 , 'upgrade' => 85 ); break;
			case 4: $guide = array( 'staff' => 5 , 'upgrade' => 100 ); break;
			case 5: $guide = array( 'staff' => 6 , 'upgrade' => 105 ); break;
			case 6: $guide = array( 'staff' => 7 , 'upgrade' => 110 ); break;
			case 7: $guide = array( 'staff' => 8 , 'upgrade' => 115 ); break;
			case 8: $guide = array( 'staff' => 9 , 'upgrade' => 120 ); break;
			case 9: $guide = array( 'staff' => 10 , 'upgrade' => 125 ); break;
			case 10: $guide = array( 'staff' => 11 , 'upgrade' => 130 ); break;
			case 11: $guide = array( 'staff' => 12 , 'upgrade' => 135 ); break;
			case 12: $guide = array( 'staff' => 13 , 'upgrade' => 140 ); break;
			case 13: $guide = array( 'staff' => 14 , 'upgrade' => 145 ); break;
			case 14: $guide = array( 'staff' => 15 , 'upgrade' => 155 ); break;
			case 15: $guide = array( 'staff' => 16 , 'upgrade' => 195 ); break;
			case 16: $guide = array( 'staff' => 17 , 'upgrade' => 200 ); break;
			case 17: $guide = array( 'staff' => 18 , 'upgrade' => 205 ); break;
			case 18: $guide = array( 'staff' => 19 , 'upgrade' => 210 ); break;
			case 19: $guide = array( 'staff' => 20 , 'upgrade' => 215 ); break;
			case 20: $guide = array( 'staff' => 21 , 'upgrade' => 220 ); break;
			case 21: $guide = array( 'staff' => 22 , 'upgrade' => 225 ); break;
			case 22: $guide = array( 'staff' => 23 , 'upgrade' => 240 ); break;
			case 23: $guide = array( 'staff' => 24 , 'upgrade' => 330 ); break;
			case 24: $guide = array( 'staff' => 25 , 'upgrade' => 350 ); break;
			case 25: $guide = array( 'staff' => 26 , 'upgrade' => 370 ); break;
			case 26: $guide = array( 'staff' => 27 , 'upgrade' => 390 ); break;
			case 27: $guide = array( 'staff' => 28 , 'upgrade' => 410 ); break;
			case 28: $guide = array( 'staff' => 29 , 'upgrade' => 430 ); break;
			case 29: $guide = array( 'staff' => 30 , 'upgrade' => 450 ); break;
			case 30: $guide = array( 'staff' => 31 , 'upgrade' => 600 ); break;
			case 31: $guide = array( 'staff' => 32 , 'upgrade' => 630 ); break;
			case 32: $guide = array( 'staff' => 33 , 'upgrade' => 660 ); break;
			case 33: $guide = array( 'staff' => 34 , 'upgrade' => 690 ); break;
			case 34: $guide = array( 'staff' => 35 , 'upgrade' => 710 ); break;
			case 35: $guide = array( 'staff' => 36 , 'upgrade' => 740 ); break;
			case 36: $guide = array( 'staff' => 37 , 'upgrade' => 950 ); break;
			case 37: $guide = array( 'staff' => 38 , 'upgrade' => 1030 ); break;
			case 38: $guide = array( 'staff' => 39 , 'upgrade' => 1150 ); break;
			case 39: $guide = array( 'staff' => 40 , 'upgrade' => 1210 ); break;
			case 40: $guide = array( 'staff' => 41 , 'upgrade' => 1260 ); break;
			case 41: $guide = array( 'staff' => 42 , 'upgrade' => 1400 ); break;
			case 42: $guide = array( 'staff' => 43 , 'upgrade' => 1440 ); break;
			case 43: $guide = array( 'staff' => 44 , 'upgrade' => 1480 ); break;
			case 44: $guide = array( 'staff' => 45 , 'upgrade' => 1540 ); break;
			case 45: $guide = array( 'staff' => 46 , 'upgrade' => 1620 ); break;
			case 46: $guide = array( 'staff' => 47 , 'upgrade' => 1820 ); break;
			case 47: $guide = array( 'staff' => 48 , 'upgrade' => 1870 ); break;
			case 48: $guide = array( 'staff' => 49 , 'upgrade' => 1930 ); break;
			case 49: $guide = array( 'staff' => 50 , 'upgrade' => 1990 ); break;
			case 50: $guide = array( 'staff' => 51 , 'upgrade' => 2050 ); break;
			case 51: $guide = array( 'staff' => 52 , 'upgrade' => 2320 ); break;
			case 52: $guide = array( 'staff' => 53 , 'upgrade' => 2380 ); break;
			case 53: $guide = array( 'staff' => 54 , 'upgrade' => 2430 ); break;
			case 54: $guide = array( 'staff' => 55 , 'upgrade' => 2500 ); break;
			case 55: $guide = array( 'staff' => 56 , 'upgrade' => 2570 ); break;
			case 56: $guide = array( 'staff' => 57 , 'upgrade' => 2830 ); break;
			case 57: $guide = array( 'staff' => 58 , 'upgrade' => 2920 ); break;
			case 58: $guide = array( 'staff' => 59 , 'upgrade' => 3000 ); break;
			case 59: $guide = array( 'staff' => 60 , 'upgrade' => 3080 ); break;
			case 60: $guide = array( 'staff' => 61 , 'upgrade' => 3170 ); break;
			case 61: $guide = array( 'staff' => 62 , 'upgrade' => 3500 ); break;
			case 62: $guide = array( 'staff' => 63 , 'upgrade' => 3570 ); break;
			case 63: $guide = array( 'staff' => 64 , 'upgrade' => 3640 ); break;
			case 64: $guide = array( 'staff' => 65 , 'upgrade' => 3710 ); break;
			case 65: $guide = array( 'staff' => 66 , 'upgrade' => 3780 ); break;
			case 66: $guide = array( 'staff' => 67 , 'upgrade' => 4200 ); break;
			case 67: $guide = array( 'staff' => 68 , 'upgrade' => 4260 ); break;
			case 68: $guide = array( 'staff' => 69 , 'upgrade' => 4330 ); break;
			case 69: $guide = array( 'staff' => 70 , 'upgrade' => 4410 ); break;
			case 70: $guide = array( 'staff' => 71 , 'upgrade' => 4500 ); break;
			case 71: $guide = array( 'staff' => 72 , 'upgrade' => 5000 ); break;
			case 72: $guide = array( 'staff' => 73 , 'upgrade' => 5060 ); break;
			case 73: $guide = array( 'staff' => 74 , 'upgrade' => 5120 ); break;
			case 74: $guide = array( 'staff' => 75 , 'upgrade' => 5160 ); break;
			case 75: $guide = array( 'staff' => 76 , 'upgrade' => 5260 ); break;
			case 76: $guide = array( 'staff' => 77 , 'upgrade' => -1 ); break;
		}
		return $guide;
	}
	/**
	 * 升级奖励
	 */
	public static function getLvAward($level){
		$award = array();
		
		switch ($level) {
			case 3: $award = array('award' => array('gold' => 8000 , 'recruit' => array(0 , 2) , 'material' => array(0 , 2)),
				'next' => 7,
				);break;
			case 7: $award = array('award' => array('gold' => 10000 , 'recruit' => array(0 , 2) , 'material' => array(0 , 2)),
				'next' => 10,
				);break;
			case 10: $award = array('award' => array('gold' => 10000 , 'recruit' => array(0 , 2) , 'material' => array(0 , 2)),
				'next' => 13,
				);break;
			case 13: $award = array('award' => array('gold' => 14000 , 'recruit' => array(0 , 2) , 'material' => array(0 , 2)),
				'next' => 15,
				);break;
			case 15: $award = array('award' => array('gold' => 16000 , 'recruit' => array(0 , 2) , 'material' => array(0 , 2)),
				'next' => 17,
				);break;
			case 17: $award = array('award' => array('gold' => 18000 , 'recruit' => array(0 , 2) , 'material' => array(0 , 2)),
				'next' => 19,
				);break;
			case 19: $award = array('award' => array('gold' => 20000 , 'recruit' => array(0 , 2) , 'material' => array(0 , 2)),
				'next' => 21,
				);break;
			case 21: $award = array('award' => array('gold' => 22000 , 'recruit' => array(0 , 2) , 'material' => array(0 , 2)),
				'next' => 23,
				);break;
			case 23: $award = array('award' => array('gold' => 24000 , 'recruit' => array(0 , 2) , 'material' => array(0 , 2)),
				'next' => 25,
				);break;
			case 25: $award = array('award' => array('gold' => 26000 , 'recruit' => array(0 , 2) , 'material' => array(0 , 2)),
				'next' => 27,
				);break;
			case 27: $award = array('award' => array('gold' => 28000 , 'recruit' => array(0 , 2) , 'material' => array(0 , 2)),
				'next' => 29,
				);break;
			case 29: $award = array('award' => array('gold' => 30000 , 'recruit' => array(0 , 2) , 'material' => array(0 , 2)),
				'next' => 31,
				);break;
			case 31: $award = array('award' => array('gold' => 32000 , 'recruit' => array(0 , 2) , 'material' => array(0 , 2)),
				'next' => 33,
				);break;
			case 33: $award = array('award' => array('gold' => 34000 , 'recruit' => array(0 , 2) , 'material' => array(0 , 2)),
				'next' => 35,
				);break;
			case 35: $award = array('award' => array('gold' => 36000 , 'recruit' => array(0 , 2) , 'material' => array(0 , 2)),
				'next' => 37,
				);break;
			case 37: $award = array('award' => array('gold' => 38000 , 'recruit' => array(0 , 2) , 'material' => array(0 , 2)),
				'next' => 39,
				);break;
			case 39: $award = array('award' => array('gold' => 40000 , 'recruit' => array(0 , 2) , 'material' => array(0 , 2)),
				'next' => 41,
				);break;
			case 41: $award = array('award' => array('gold' => 42000 , 'recruit' => array(0 , 2) , 'material' => array(0 , 2)),
				'next' => 43,
				);break;
			case 43: $award = array('award' => array('gold' => 44000 , 'recruit' => array(0 , 2) , 'material' => array(0 , 2)),
				'next' => 45,
				);break;
			case 45: $award = array('award' => array('gold' => 46000 , 'recruit' => array(0 , 2) , 'material' => array(0 , 2)),
				'next' => 47,
				);break;
			case 47: $award = array('award' => array('gold' => 48000 , 'recruit' => array(0 , 2) , 'material' => array(0 , 2)),
				'next' => 49,
				);break;
			case 49: $award = array('award' => array('gold' => 50000 , 'recruit' => array(0 , 2) , 'material' => array(0 , 2)),
				'next' => 51,
				);break;
			case 51: $award = array('award' => array('gold' => 52000 , 'recruit' => array(0 , 2) , 'material' => array(0 , 2)),
				'next' => 53,
				);break;
			case 53: $award = array('award' => array('gold' => 54000 , 'recruit' => array(0 , 2) , 'material' => array(0 , 2)),
				'next' => 55,
				);break;
			case 55: $award = array('award' => array('gold' => 56000 , 'recruit' => array(0 , 2) , 'material' => array(0 , 2)),
				'next' => 57,
				);break;
			case 57: $award = array('award' => array('gold' => 58000 , 'recruit' => array(0 , 2) , 'material' => array(0 , 2)),
				'next' => 59,
				);break;
			case 59: $award = array('award' => array('gold' => 60000 , 'recruit' => array(0 , 2) , 'material' => array(0 , 2)),
				'next' => 61,
				);break;
			case 61: $award = array('award' => array('gold' => 62000 , 'recruit' => array(0 , 2) , 'material' => array(0 , 2)),
				'next' => 63,
				);break;
			case 63: $award = array('award' => array('gold' => 64000 , 'recruit' => array(0 , 2) , 'material' => array(0 , 2)),
				'next' => 65,
				);break;
			case 65: $award = array('award' => array('gold' => 66000 , 'recruit' => array(0 , 2) , 'material' => array(0 , 2)),
				'next' => 67,
				);break;
			case 67: $award = array('award' => array('gold' => 68000 , 'recruit' => array(0 , 2) , 'material' => array(0 , 2)),
				'next' => 69,
				);break;
			case 69: $award = array('award' => array('gold' => 70000 , 'recruit' => array(0 , 2) , 'material' => array(0 , 2)),
				'next' => 71,
				);break;
			case 71: $award = array('award' => array('gold' => 72000 , 'recruit' => array(0 , 2) , 'material' => array(0 , 2)),
				'next' => 73,
				);break;
			case 73: $award = array('award' => array('gold' => 74000 , 'recruit' => array(0 , 2) , 'material' => array(0 , 2)),
				'next' => 75,
				);break;
			case 75: $award = array('award' => array('gold' => 76000 , 'recruit' => array(0 , 2) , 'material' => array(0 , 2)),
				'next' => 77,
				);break;
		}
		return $award;
	}
	/**
	 * url跳转
	 * Enter description here ...
	 */
	public static function getHeader($url){
		header( 'Location: ' . $url . '&sid=' . sessionInfo('skey') . '&r=' .rand(1, 10000));
	}
	/**
	 * 武将等级经验列表
	 */
	public static function getGeneralsExp($level){
		$guide = array();
		
		switch ($level) {
			case 1: $guide = array( 'staff' => 2 , 'upgrade' => 30 ); break;
			case 2: $guide = array( 'staff' => 3 , 'upgrade' => 40 ); break;
			case 3: $guide = array( 'staff' => 4 , 'upgrade' => 50 ); break;
			case 4: $guide = array( 'staff' => 5 , 'upgrade' => 65 ); break;
			case 5: $guide = array( 'staff' => 6 , 'upgrade' => 80 ); break;
			case 6: $guide = array( 'staff' => 7 , 'upgrade' => 90 ); break;
			case 7: $guide = array( 'staff' => 8 , 'upgrade' => 105 ); break;
			case 8: $guide = array( 'staff' => 9 , 'upgrade' => 120 ); break;
			case 9: $guide = array( 'staff' => 10 , 'upgrade' => 135 ); break;
			case 10: $guide = array( 'staff' => 11 , 'upgrade' => 150 ); break;
			case 11: $guide = array( 'staff' => 12 , 'upgrade' => 170 ); break;
			case 12: $guide = array( 'staff' => 13 , 'upgrade' => 185 ); break;
			case 13: $guide = array( 'staff' => 14 , 'upgrade' => 200 ); break;
			case 14: $guide = array( 'staff' => 15 , 'upgrade' => 220 ); break;
			case 15: $guide = array( 'staff' => 16 , 'upgrade' => 240 ); break;
			case 16: $guide = array( 'staff' => 17 , 'upgrade' => 260 ); break;
			case 17: $guide = array( 'staff' => 18 , 'upgrade' => 275 ); break;
			case 18: $guide = array( 'staff' => 19 , 'upgrade' => 290 ); break;
			case 19: $guide = array( 'staff' => 20 , 'upgrade' => 310 ); break;
			case 20: $guide = array( 'staff' => 21 , 'upgrade' => 330 ); break;
			case 21: $guide = array( 'staff' => 22 , 'upgrade' => 350 ); break;
			case 22: $guide = array( 'staff' => 23 , 'upgrade' => 375 ); break;
			case 23: $guide = array( 'staff' => 24 , 'upgrade' => 400 ); break;
			case 24: $guide = array( 'staff' => 25 , 'upgrade' => 425 ); break;
			case 25: $guide = array( 'staff' => 26 , 'upgrade' => 450 ); break;
			case 26: $guide = array( 'staff' => 27 , 'upgrade' => 475 ); break;
			case 27: $guide = array( 'staff' => 28 , 'upgrade' => 495 ); break;
			case 28: $guide = array( 'staff' => 29 , 'upgrade' => 520 ); break;
			case 29: $guide = array( 'staff' => 30 , 'upgrade' => 545 ); break;
			case 30: $guide = array( 'staff' => 31 , 'upgrade' => 570 ); break;
			case 31: $guide = array( 'staff' => 32 , 'upgrade' => 600 ); break;
			case 32: $guide = array( 'staff' => 33 , 'upgrade' => 630 ); break;
			case 33: $guide = array( 'staff' => 34 , 'upgrade' => 650 ); break;
		}
		return $guide;
	}
	/**
	 * 获得几率
	 */
	public static function getChance($percentage){
		//检查$percentage是否在1~100之间
		if(($percentage>=1)&&($percentage<=100))
		{
		//取得1~100之间的乱数
		//(这里采用mt_rand而不是rand函数是因为PHP官网说mt_rand函数比rand函数有效率)
		$rand=mt_rand(1,100);
		
		//如果数字小于$percentage
		if($rand<=$percentage)
		{
		//成立
		$kk = 8;
		return true;
		}
		
		//不成立
		return false;
		}
		
		//数字并非在1~100之间
		return false;
	}
	/**
	 * 获取更新武将
	 * Enter description here ...
	 */
	public static function getStrtotime(){
		//按时间刷新
		$day = date('Y-m-d');
		$time = isset($_SERVER['REQUEST_TIME']) ? $_SERVER['REQUEST_TIME'] : time();
		$time_str = date('His' , $time);
		
		if( $time_str <= '020000') {
			$next_time = strtotime( $day . ' 02:00:00' );
		} else if( $time_str > '020000' && $time_str <= '080000' ) {
			$next_time = strtotime( $day . ' 08:00:00' );
		}  else if( $time_str > '080000' && $time_str <= '140000' ) {
			$next_time = strtotime( $day . ' 14:00:00' );
		}  else if( $time_str > '140000' && $time_str <= '200000' ) {
			$next_time = strtotime( $day . ' 20:00:00' );
		} else {
			 // > 20
			$next_time = strtotime( $day . ' 02:00:00' ) + 86400;
		}

		return $next_time;
	}
	/**
	 * 消费记录
	 */
	public static function getConsume($information){
		$uin = SessionInfo('uin');
		loadLib('App_Overlord_Query');
		//查询动态记录
		$consume_info = App_Overlord_Query::oneConsume($uin);
		
		if (empty($consume_info)){
			$consume_info['Value'][] = $information;
			$code = App_Overlord_Query::addConsume($uin, $consume_info['Value']);
		}else{
			$count = count($consume_info['Value']);
		
			if ($count >= 20){
				//初始化信息记录
				$up_consume_info = array();
				foreach ($consume_info['Value'] as $k => $row){
					if ($k != 0){
						$up_consume_info['Value'][] = $row;
					}
				}
			}
			//超过20条
			if (!empty($up_consume_info)){
				$up_count = count($up_consume_info['Value']);
				
				$up_consume_info['Value'][$up_count] = $information;
				$code = App_Overlord_Query::alterConsume($uin, $up_consume_info['Value']);
			}else{
				
				$consume_info['Value'][$count] = $information;
				
				$code = App_Overlord_Query::alterConsume($uin, $consume_info['Value']);
			}
		}
	}
	/**
	 * 情报记录
	 */
	public static function getCharacters($c_uin , $introduce){
		loadLib('App_Overlord_Query');
		//查询攻击记录
		$characters_info = App_Overlord_Query::oneCollocate('app_overlord_characters', 'characters_' .$c_uin);
		if (empty($characters_info)){
			$characters_info['Value'][] = $introduce;
			$code = App_Overlord_Query::addCollocate('app_overlord_characters', 'characters_' .$c_uin, $characters_info['Value']);
		}else{
			$count = count($characters_info['Value']);
			if ($count >= 20){
				//初始化信息记录
				$up_characters_info = array();
				foreach ($characters_info['Value'] as $k => $row){
					if ($k != 0){
						$up_characters_info['Value'][] = $row;
					}
				}
			}
		//超过20条
			if (!empty($up_characters_info)){
				$up_count = count($up_characters_info['Value']);
				$up_characters_info['Value'][$up_count] = $introduce;
				$code = App_Overlord_Query::alterCollocate('app_overlord_characters', 'characters_' . $c_uin, $up_characters_info['Value']);
			}else{
				$characters_info['Value'][$count] = $introduce;
				$code = App_Overlord_Query::alterCollocate('app_overlord_characters', 'characters_' . $c_uin, $characters_info['Value']);
			}
		}
	}
	/**
	 * 游戏帮助
	 */
	public static function getFaq(){
		$issue = array('君主问题' ,
						 '将领问题' ,
						 '士兵问题' ,
						 '主城问题' ,
						 '点将台问题' ,
						 '兵营问题' ,
						 '训练场问题' ,
						 '兵器司问题' ,
						 '建筑问题' ,
						 '战斗问题'	,
						 '道具问题' ,
						 '宝箱问题' ,
						 '货币问题' ,
						 '其他问题' 
					);
		return $issue;
	}
	/**
	 * 详细问题
	 */
	public static function getIssue(){
		$issue = array( 0 => array('君主等级作用' , '怎样提升君主等级'),
						1 => array('等级' , '属性' , '装备' , '技能'),
						2 => array('普通士兵' , '天界奇兵'),
						10 => array('加速道具' , '装备道具' , '刷新道具' , '其他道具'),
						11 => array('宝箱' , '钥匙'),
						12 => array('黄金' , '功勋' , '钻石'),
					);
		return $issue;
	}
	/**
	 * 武将升级加属性
	 */
	public static function getGeneralsPromotion ($info , $generals_lv){
		//武将升级
		$info['exp'] -= $generals_lv['upgrade'];
		$info['lv'] = $generals_lv['staff'];
		$info['ap'] += $info['grow_ap'];
		$info['hp'] += $info['grow_hp'];
		$info['defense'] += $info['grow_defense'];
		//查询武将可带兵数
		$is_count = self::getGeneralsBarracksLv($info['lv']);
		
		//武器攻击初始
		$grow_ap_num = 0;
		if (!empty($info['equipment']['weapon']['num'])){
			$grow_ap_num = $info['equipment']['weapon']['num'];
		}
		//盔甲防御初始
		$grow_defense_num = 0;
		if (!empty($info['equipment']['armour']['num'])){
			$grow_defense_num = $info['equipment']['armour']['num'];
		}
		//头盔
		$grow_hp_num = 0;
		if (!empty($info['equipment']['helm']['num'])){
			$grow_hp_num = $info['equipment']['helm']['num'];
		}
		//初始化武将带兵数
		$make_count = $is_count['count'];
		//初始化攻击
		$grow_ap = $info['ap'] - $grow_ap_num;
		//初始化防御力
		$grow_defense = $info['defense'] - $grow_defense_num;
		//初始化生命力
		$grow_hp = $info['hp'] - $grow_hp_num;
		//加载技能列表
		loadLib('App_Overlord_Collocate');
		$skills = App_Overlord_Collocate::getSkill();
		//统御
		if (!empty($info['skills']['strengthen'])){
			//如果有加兵技能
			foreach ($info['skills']['strengthen'] as $k => $skills_row){
				if ($k == 'staff'){
					//带兵加成
					$skills_num = $skills['strengthen'][$k][$skills_row]['num'];
					$make_count += $make_count * $skills_num/100;
				}
			}
		}
		//武技
		if (!empty($info['skills']['study'])){
			//如果有加兵技能
			foreach ($info['skills']['study'] as $k => $skills_row){
				if ($k == 'ap'){
					//攻击力加成
					$skills_num = $skills['study'][$k][$skills_row]['num'];
					$grow_ap += $grow_ap * $skills_num/100;
				}elseif ($k == 'defense'){
					//防御力加成
					$skills_num = $skills['study'][$k][$skills_row]['num'];
					$grow_defense += $grow_defense * $skills_num/100;
				}elseif ($k == 'hp'){
					//防御力加成
					$skills_num = $skills['study'][$k][$skills_row]['num'];
					$grow_hp += $grow_hp * $skills_num/100;
				}
			}
		}
		//战马
		if (!empty($info['equipment']['horse']['num'])){
			$make_count += $info['equipment']['horse']['num'];
		}
		//武器攻击初始
		if (!empty($info['equipment']['weapon']['num'])){
			$grow_ap += $grow_ap_num;
		}
		//盔甲防御初始
		if (!empty($info['equipment']['armour']['num'])){
			$grow_defense += $grow_defense_num;
		}
		//头盔
		if (!empty($info['equipment']['helm']['num'])){
			$grow_hp += $grow_hp_num;
		}
		$info['ap'] = $grow_ap;
		$info['defense'] = $grow_defense;
		$info['hp'] = $grow_hp;
		$info['make_count'] = $make_count;
		
		return $info;
	}
	/**
	 * 战斗挑衅语
	 * Enter description here ...
	 */
	public static function provoke(){
		$fight = array(
				'失败的滋味,喜欢吗?',
				'小样!再去练练吧!',
				'我了个擦!你也太菜了!',
				'坑爹的!才三招就挂了?!',
				'你!不是哥的对手!'
		);
		return $fight;
	}
}
# end of script